import Foundation

struct Joke: Codable {
    let id: Int
    let category: Category
    let type: JokeType
    let lang: Language
    let setup: String?
    let delivery: String?
    let joke: String?
}

enum Category: String, Codable, CaseIterable, Identifiable {
    case `Any`, Programming, Misc, Dark, Pun, Spooky, Christmas
    var id: Self { self }
}

enum Language: String, Codable, CaseIterable, Identifiable {
    case en, fr, cs, de, es, pt
    var id: Self { self }
}

enum JokeType: String, Codable {
    case twopart, single
}



/*
 Possible Endpoints
 Any category, English
 https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit
 
 Single category, English
 https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit
 
 Any Category, language other than english
 https://v2.jokeapi.dev/joke/Any?lang=fr&blacklistFlags=nsfw,religious,political,racist,sexist,explicit
 
 Single Category, language other than english
 https://v2.jokeapi.dev/joke/Programming?lang=fr&blacklistFlags=nsfw,religious,political,racist,sexist,explicit
 
 */

func getJoke() async throws -> Joke {
//    let url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit"
//    let url = "https://v2.jokeapi.dev/joke/\(Category.Pun)?blacklistFlags=nsfw,religious,political,racist,sexist,explicit"
//    let url = "https://v2.jokeapi.dev/joke/Any?lang=\(Language.cs)&blacklistFlags=nsfw,religious,political,racist,sexist,explicit"
    let url = "https://v2.jokeapi.dev/joke/\(Category.Misc)?lang=\(Language.fr)&blacklistFlags=nsfw,religious,political,racist,sexist,explicit"
    let apiService = APIService(urlString: url)
    do {
        return try await apiService.getJSON()
    } catch {
        throw error
    }
}

Task {
    do {
        let joke = try await getJoke()
        print(joke.setup ?? "")
        print(joke.delivery ?? "")
        print(joke.joke ?? "")
    } catch {
        print(error.localizedDescription)
    }
}
